package vn.com.demand.lux;

import java.util.StringTokenizer;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import vn.com.demand.lux.preferences.PreferenceConstants;
import vn.com.demand.util.DpUtils;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "LuxPayment";
	
	// The shared instance
	private static Activator plugin;
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
	
	public String[] getDefaultDatabaseProperties() {
		return convert(PreferenceConstants.DEFAULT_DATABASE_PROPERTIES);
	}
	
	public String[] getDatabaseProperties() {
		return convert(getPreferenceStore().getString(PreferenceConstants.DATABASE_CONNECTION));
	}
	
	public void setDatabasePropertiesPreferences(String[] elements) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < elements.length; i++) {
			buffer.append(elements[i]);
			buffer.append(";");
		}
		getPreferenceStore().setValue(PreferenceConstants.DATABASE_CONNECTION, buffer.toString());
	}
	
	private String[] convert(String value) {
		StringTokenizer token = new StringTokenizer(value, ";");
		int count = token.countTokens();
		String[] elements = new String[count];
		for (int i = 0; i < count; i++) {
			elements[i] = token.nextToken();
		}
		return elements;
	}
	
	// Date Format Preference
	public String getDefaultsDateFormatType() {
		return PreferenceConstants.DEFAULT_DATE_FORMAT;
	}
	
	public String getDateFormatTypePreferences() {
		String dateFormat = getPreferenceStore().getString(PreferenceConstants.DATE_FORMAT).trim();
		return DpUtils.length(dateFormat) > 0 ? dateFormat : getDefaultsDateFormatType();
	}
	
	public void setDateFormatTypePreferences(String dateFormat) {
		String format = getPreferenceStore().getString(PreferenceConstants.DATE_FORMAT).trim();
		if (null == format) {
			getPreferenceStore().setValue(PreferenceConstants.DATE_FORMAT, PreferenceConstants.DEFAULT_DATE_FORMAT);
		} else {
			getPreferenceStore().setValue(PreferenceConstants.DATE_FORMAT, dateFormat);
		}
	}
	
	public String getAccountReceivableXSLMapping() {
		return getPreferenceStore().getString(PreferenceConstants.XSL_ACCOUNTRECEIVABLEMAPPING);
	}
	
	public String getInterestForwardXSLMapping() {
		return getPreferenceStore().getString(PreferenceConstants.XSL_INTERESTFORWARD);
	}
	
	public String getWarrantyAccrualXSLMapping() {
		return getPreferenceStore().getString(PreferenceConstants.XSL_WARRANTYACCRUAL);
	}
	
	public String getWarrantyReservalXSLMapping() {
		return getPreferenceStore().getString(PreferenceConstants.XSL_WARRANTYRESERVAL);
	}
	
	public String getLastedAccountReceivableDivision() {
		return getPreferenceStore().getString(PreferenceConstants.ACCOUNT_RECEIVABLE_DIVISION);
	}
	
	public String getLastedAccountReceivableJounalCode() {
		return getPreferenceStore().getString(PreferenceConstants.ACCOUNT_RECEIVABLE_JOURNAL_CODE);
	}
	
	public String getLastedAccountReceivableRevenue() {
		return getPreferenceStore().getString(PreferenceConstants.ACCOUNT_RECEIVABLE_REVENUE);
	}
	
	public String getLastedAccountReceivableVatAmount() {
		return getPreferenceStore().getString(PreferenceConstants.ACCOUNT_RECEIVABLE_VAT_AMOUNT);
	}
	
	public String getLastedAccountReceivableInterest() {
		return getPreferenceStore().getString(PreferenceConstants.ACCOUNT_RECEIVABLE_INTEREST);
	}
	
	public String getLastedAccountReceivableBookingFee() {
		return getPreferenceStore().getString(PreferenceConstants.ACCOUNT_RECEIVABLE_BOOKING_FEE);
	}
	
	public String getLastedAccountReceivableDownPayment() {
		return getPreferenceStore().getString(PreferenceConstants.ACCOUNT_RECEIVABLE_DOWNPAYMENT);
	}

	public String getLastedAccountReceivableInstallmentAmount() {
		return getPreferenceStore().getString(PreferenceConstants.ACCOUNT_RECEIVABLE_INSTALLMENT_AMOUNT);
	}
	
	public String getAccountReceivableXMLLocation() {
		return getPreferenceStore().getString(PreferenceConstants.XML_ACCOUNTRECEIVABLE);
	}
	
}
